package com.sly.fuxi.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import com.sly.fuxi.rabbitmq.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author SLY
 * @date 2025/2/15
 */
@Slf4j
@Component
public class RabbitMQConsumer {
    /**
     * 直连交换机
     */
    @RabbitListener(queues = RabbitMQConfig.DIRECT_QUEUE_1)
    public void receiveDirectQueue1(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveDirectQueue1:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveDirectQueue1:", e);
        }
    }

    /**
     * 直连交换机
     */
    @RabbitListener(queues = RabbitMQConfig.DIRECT_QUEUE_2)
    public void receiveDirectQueue2(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveDirectQueue2:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveDirectQueue2:", e);
        }
    }

    /**
     * 直连交换机
     */
    @RabbitListener(queues = RabbitMQConfig.DIRECT_QUEUE_3)
    public void receiveDirectQueue3(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveDirectQueue3:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveDirectQueue3:", e);
        }
    }

    /**
     * 扇形交换机
     */
    @RabbitListener(queues = RabbitMQConfig.FANOUT_QUEUE_1)
    public void receiveFanoutQueue11(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveFanoutQueue1_1:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveFanoutQueue1_1:", e);
        }
    }

    /**
     * 扇形交换机
     */
    @RabbitListener(queues = RabbitMQConfig.FANOUT_QUEUE_1)
    public void receiveFanoutQueue12(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveFanoutQueue1_2:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveFanoutQueue1_2:", e);
        }
    }

    /**
     * 扇形交换机
     */
    @RabbitListener(queues = RabbitMQConfig.FANOUT_QUEUE_2)
    public void receiveFanoutQueue2(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveFanoutQueue2:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveFanoutQueue2:", e);
        }
    }

    /**
     * 主题交换机
     */
    @RabbitListener(queues = RabbitMQConfig.TOPIC_QUEUE_1)
    public void receiveTopicQueue1(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveTopicQueue1:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveTopicQueue1:", e);
        }
    }

    /**
     * 主题交换机
     */
    @RabbitListener(queues = RabbitMQConfig.TOPIC_QUEUE_2)
    public void receiveTopicQueue2(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveTopicQueue2:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveTopicQueue2:", e);
        }
    }

    /**
     * 主题交换机
     */
    @RabbitListener(queues = RabbitMQConfig.TOPIC_QUEUE_3)
    public void receiveTopicQueue3(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveTopicQueue3:{}", message);
            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveTopicQueue3:", e);
        }
    }

    /**
     * 延时队列
     */
    @RabbitListener(queues = RabbitMQConfig.DELAYED_QUEUE)
    public void receiveDelayedQueue(String message, Message amqpMessage, Channel channel) throws IOException {
        try {
            log.info("receiveDelayedQueue:{}", message);

            channel.basicAck(amqpMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(amqpMessage.getMessageProperties().getDeliveryTag(), false, false);
            log.error("receiveDelayedQueue:", e);
        }
    }
}
